/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package beans;

import entities.Candidate;
import entities.Company;
import entities.Notification;
import entities.PublicRelations;
import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
import java.io.Serializable;
import java.util.Locale;
import java.util.ResourceBundle;
import javax.annotation.PostConstruct;
import javax.enterprise.event.Observes;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import qualifiers.HeaderClickedEvent;
import qualifiers.CreatedEntityEvent;
import qualifiers.EditedEntityEvent;
import qualifiers.LoggedIn;

/**
 *
 * @author damfercer
 */
@Named
@SessionScoped
public class BarBean implements Serializable {
    
private Integer activeIndex;
private Integer candidateNotifications = 0;
@Inject
@LoggedIn
private Candidate candidate;
    /**
     * Creates a new instance of BarBean
     */
    public BarBean() {
        activeIndex=0;
    }
    
    @PostConstruct
    public void init(){
        if(candidate!=null){
            for(Notification notification : candidate.getUser().getNotificationList()){
                if(!notification.getIsNotificated()){
                    candidateNotifications++;
                }
            }
        }
    }
    
    public void onNewNotification(@Observes @CreatedEntityEvent Notification notification){
        if(candidate!=null && notification.getUser().equals(candidate.getUser())){
            candidateNotifications++;
        }
        
    }
    public void onReadNotification(@Observes @EditedEntityEvent Notification notification){
        if(notification.getUser().equals(candidate.getUser())){
            candidateNotifications--;
        }
        
    }
    
    public String notificationsOfCandidate(){
        FacesContext context = FacesContext.getCurrentInstance();
        Locale locale=context.getViewRoot().getLocale();
        ResourceBundle rb = ResourceBundle.getBundle("messages", locale);
        String noti = rb.getString("Notifications");
        if(candidate!=null && candidateNotifications !=0){
            noti= "("+candidateNotifications.toString()+") "+noti;
        }
        return noti;
    }
    
    public String changeCandidateIndex(Integer index){
        String redirect=null;
        activeIndex=index;
        if(activeIndex==0){
            redirect="/views/candidate/IndexViewCandidate?faces-redirect=true";
        }
        if(activeIndex==1){
            redirect="/views/candidate/EditCandidateView?faces-redirect=true";
        }
        if(activeIndex==2){
            redirect="/views/candidate/BookCandidateView?faces-redirect=true";
        }
        if(activeIndex==3){
            redirect="/views/candidate/NotificationsCandidateView?faces-redirect=true";
        }
//        if(activeIndex==4){
//            redirect="/views/candidate/OffersCandidateView?faces-redirect=true";
//        }
        if(activeIndex==4){
            redirect="/views/candidate/ConfigurationCandidateView?faces-redirect=true";
        }
        return redirect;
    }
    
    public String changeCompanyIndex(Integer index){
        String redirect=null;
        activeIndex=index;
        if(activeIndex==0){
            redirect="/views/company/IndexViewCompany?faces-redirect=true";
        }
        if(activeIndex==1){
            redirect="/views/company/EditCompanyView?faces-redirect=true";
        }
        if(activeIndex==2){
            redirect="/views/company/search/SearchListView?faces-redirect=true";
        }
        if(activeIndex==3){
            redirect="/views/company/folder/FolderListView?faces-redirect=true";
        }
        if(activeIndex==4){
            redirect="/views/company/CreditsCompanyView?faces-redirect=true";
        }
        if(activeIndex==5){
            redirect="/views/company/ConfigurationCompanyView?faces-redirect=true";
        }
        return redirect;
    }
    
    
    public String changeAdminIndex(Integer index){
        String redirect=null;
        activeIndex=index;
        if(activeIndex==0){
            redirect="/views/admin/IndexViewAdmin?faces-redirect=true";
        }
        if(activeIndex==1){
            redirect="/views/admin/AdminCompanyView?faces-redirect=true";
        }
        if(activeIndex==2){
            redirect="/views/admin/AdminCandidateView?faces-redirect=true";
        }
        if(activeIndex==3){
            redirect="/views/admin/AdminPublicRelationshipsView?faces-redirect=true";
        }
        if(activeIndex==4){
            redirect="/views/admin/AdminReportCandidateView?faces-redirect=true";
        }
        if(activeIndex==5){
            redirect="/views/admin/AdminStatisticsView?faces-redirect=true";
        }
        
        return redirect;
    }
    
     public String changePublicRelationsIndex(Integer index){
        String redirect=null;
        activeIndex=index;
        if(activeIndex==0){
            redirect="/views/publicRelations/IndexViewPublicRelations?faces-redirect=true";
        }  
        if(activeIndex==1){
            redirect="/views/publicRelations/ConfigurationPublicRelationsView?faces-redirect=true";
        } 
        return redirect;
    }

    /**
     * @return the activeIndex
     */
    public Integer getActiveIndex() {
        return activeIndex;
    }

    /**
     * @param activeIndex the activeIndex to set
     */
    public void setActiveIndex(Integer activeIndex) {
        this.activeIndex = activeIndex;
    }
    
    
    public void onNewCandidate(@Observes @CreatedEntityEvent Candidate newCandidate){
        this.activeIndex=0;
    }
    
    public void onNewCompany(@Observes @CreatedEntityEvent Company newCompany){
        this.activeIndex=1;
    }
    
    public void onHeaderClick(@Observes @HeaderClickedEvent Integer index){
        this.activeIndex=index;
    }
    
    public void onLoggedInCandidate(@Observes @LoggedIn Candidate candidate){
        setActiveIndex(0);
    }
    public void onLoggedInCompany(@Observes @LoggedIn Company company){
        setActiveIndex(0);
    }
     public void onLoggedInPublicRelations(@Observes @LoggedIn PublicRelations publicRelations){
        setActiveIndex(0);
    }
}
